/**
 * @file src/TracebackMotif.cpp
 *
 * @attention positions are not in absolute distances but in additional distances
 *
 * @date 15-may-2013
 *
 * @author Youri Hoogstrate
 * @author Lisa Yu
 *
 * @section LICENSE
 * yh-kt-fold can predict RNA 2D structures including K-turns.
 * Copyright (C) 2012-2013 Youri Hoogstrate
 *
 * This file is part of yh-kt-fold.
 *
 * yh-kt-fold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * yh-kt-fold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */



#include "TracebackMotif.h"



/**
 * @brief set K-turn (or other motif) and do all the pushing
 *
 * @param bonds
 * @param size5p
 * @param size3p
 * @param direction If the search direction is forwards.
 *
 * @date 25-may-2013
 */
//void TracebackMotif::init(int arg_bonds[], int arg_size5p, int arg_size3p, bool arg_direction)
void TracebackMotif::init(int *arg_bonds, int arg_size5p, int arg_size3p, bool arg_direction)
{
	int i, j;
	
	this->tracebackStackTop = 0;
	this->tracebackDirectionForward = arg_direction;
	
	for(i = arg_size5p - 1; i >= 0; i--)								// Pushing
	{
		j = arg_bonds[i];
		
		if(j > -1)														/** @todo If j is a valid bond? => use constants!! */
		{
			if(this->tracebackDirectionForward)
			{
				this->tracebackStack[this->tracebackStackTop][0] = i;
				this->tracebackStack[this->tracebackStackTop][1] = j;
			}
			else
			{
				this->tracebackStack[this->tracebackStackTop][0] = arg_size3p - j - 1;
				this->tracebackStack[this->tracebackStackTop][1] = arg_size5p - i - 1;
			}
			this->tracebackStackTop++;
		}
	}
}



/**
 * @brief Pop from the traceback motif and copy to i an j.
 *
 * @param i Pointer to variable i
 * @param j Pointer to variable j
 *
 * @attention distances are not absolute to the entire RNA sequence but absolute to the motif's length
 *
 * @date 25-may-2013
 */
bool TracebackMotif::pop(int *i, int *j)
{
	if(this->tracebackStackTop <= 0)
	{
		return false;
	}
	else
	{
		this->tracebackStackTop--;
		
		(*i) = this->tracebackStack[tracebackStackTop][0];
		(*j) = this->tracebackStack[tracebackStackTop][1];
		
		return true;
	}
}
